import {Page} from '@/base/Page'
import {deepAssign} from '@/base/util/Objects';
import {Software} from '@/base/model/Software';
import {DeviceSort} from '@/base/model/DeviceSort';
import {DeviceModel} from '@/base/model/DeviceModel';
import {
  deleteSoftwareId,
  getSoftware,
  getSoftwareId,
  postSoftware,
  PutSoftwareId,
  getVersions
} from '@/base/api/SoftwareApi'
import {
  firmwareVersions
} from '@/base/api/FirmwareApi'
import {getDeviceModel, getDeviceModelId} from '@/base/api/DeviceModelApi'
import {getDeviceSort} from '@/base/api/DeviceSortApi'

export class SoftwareBloc {

  page: Page<DeviceSort> = new Page<DeviceSort>([], 0, 1, 15, 0, []);
  page1: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 15, 0, []);
  software: Page<Software> = new Page<Software>([], 0, 1, 10, 0, []);
  model: any = {
    describe: '',
    extName: '',
    firmwareVersions: 0,
    modelId: 0,
    sortId: 0,
    url: '',
    versions: 0 || undefined,
    issueStatus: '',
    accountName: ''
  };
  softwareId: Software = {
    accountId: 0,
    accountName: '',
    created: '',
    describe: '',
    extName: '',
    firmwareVersions: 0,
    id: 0,
    issueStatus: '',
    modelId: 0,
    sortId: 0,
    updated: '',
    url: '',
    versions: 0,
    data: [],
    productType:0
  }
  sortList: any = [];
  modelList: any = [];
  agentList: any = [];
  versions: any[] = []
  firmwareVersions: any[] = []
  firmwareId: object[] = []
  productList:any = [
    {
      id:0,
      title:'iRest云'
    },
    {
      id:1,
      title:'VR眼镜'
    },
  ]

  loading = false;

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getDeviceSort({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      deepAssign(this.page, res.data);
      this.sortList = this.page.items.map(function (item) {
        var obj = {id: 0, sortName: ''};
        // @ts-ignore
        obj.id = item.id;
        obj.sortName = item.sortName;
        return obj
      });
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getAllModel(pageIndex: number, pageSize: number, sortId: number, sortDirection: string, companyId) {
    this.loading = true;
    let that = this;
    return getDeviceModel({
      pageIndex,
      pageSize,
      sortId,
      sortDirection,
      companyId,
      id: this.model.id,
      modelName: this.model.modeName
    }).then((res) => {
      deepAssign(this.page1, res.data);
      this.modelList = this.page1.items.map(function (item) {
        var obj = {id: 0, modelName: ''};
        obj.id = item.id;
        obj.modelName = item.modelName;
        return obj
      });
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getModel(id: number) {
    this.loading = true;
    return getDeviceModelId({id}).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  addSoftware(
    describe: string,
    modelId: number,
    sortId: number,
    url: string,
    versions: number,
    issueStatus: string,
    productType: number) {
    this.loading = true;
    return postSoftware({
      describe,
      extName: this.model.extName,
      firmwareVersions: this.model.firmwareVersions,
      modelId,
      sortId,
      url,
      versions,
      issueStatus,
      productType
    }).then((res) => {
      this.getSoftware(this.software.pageIndex, 10, 'DESC', this.model)
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getSoftware(
    pageIndex: number, pageSize: number, sortDirection: string, model: any) {
    this.loading = true;
    if (model.modelId === 0) {
      model.modelId = undefined
    }
    return getSoftware({
      pageIndex: pageIndex,
      pageSize: pageSize,
      sortDirection: sortDirection,
      accountName: model.accountName,
      versions: model.versions,
      issueStatus: model.issueStatus,
      modelId: model.modelId
    }).then((res) => {
      deepAssign(this.software, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getfirmware() {
    return firmwareVersions({modelId: this.model.modelId}).then((res) => {
      this.firmwareVersions = res.data
    }).finally(() => {
      this.loading = false;
    })
  }

  getVersions() {
    this.loading = true;
    return getVersions().then((res) => {
      this.versions = res.data.map(function (item, index) {
        let obj = {index: 0, version: ''}
        obj.index = index
        // @ts-ignore
        obj.version = item
        return obj
      })
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  toModifyItem(id: number) {
    this.loading = true;
    return getSoftwareId(
      id
    ).then((res) => {
      deepAssign(this.softwareId, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  modifyItem(describe: string,
             id: number,
             issueStatus: string,
             modelId: number,
             sortId: number,
             url: string,
             productType: number) {
    this.loading = true;
    return PutSoftwareId({
      describe,
      extName: this.softwareId.extName,
      firmwareVersions: this.softwareId.firmwareVersions,
      id,
      issueStatus,
      modelId,
      sortId,
      url,
      productType
    }).then((res) => {
      deepAssign(this.softwareId, res.data);
      this.getSoftware(this.software.pageIndex, 10, 'DESC', this.model)
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  removeItem(dateItem: Software) {
    this.loading = true;
    return deleteSoftwareId(dateItem.id)
      .then((res) => {
        this.getSoftware(this.software.pageIndex, 10, 'DESC', this.model)
        return res;
      })
      .finally(() => {
        this.loading = false;
      })
  }

  getFirmWareName(firmwareId: number) {
    const item = this.software.extras.find(x => x.id === firmwareId);
    return item == null ? '' : item.versions;
  }

}
